<?php
/**
 * create by phpstorm
 * User: 冰帝
 * Date: 2023/5/4
 * Remark: 登录接口
 * 现在的努力是为了小时候吹过的牛逼
 */

namespace app\api\controller;


use app\BaseController;
use think\exception\ValidateException;
use think\facade\Db;

class GoodHealth extends AuthBase
{
    protected $noLogin = ['index','phone_code'];

    public function index(){
        $user=Db::name('user_plan')
            ->where('user_id',$this->userId)
            ->find();
        if (!$user){
            $list=Db::name('smoke_stage')
                ->select()
                ->toArray();
            $model['list']=$list;
            return show(1,'',$model);
        }else{

            //如果已经开始戒烟了,那就返回戒烟数据
            $num=Db::name('user_plan_record')
                ->where('user_id',$this->userId)
                ->count();
            if ($user['type']==1&&$num>=7){
                return show(100,"请购买会员");
            }
            //计算少吸和少花费
            $smoke_num=$user['smoke_num']*$num;
            $cost=$user['cost']*$num;
            //计算健康值
            $model=Db::name('article')
                ->where('type',2)
                ->order('id','asc')
                ->column('id,title,day,content');
            $one=0;
            foreach ($model as $k=>$v){
                $bai=round(($num/$v['day']*100),2);
                if ($bai>100){
                    $bai=100;
                }
                $model[$k]['baie']=$bai;
                $one+=$bai;
                $model[$k]['content'] = mb_substr(strip_tags($v['content']), 0, 20, 'UTF-8');
            }
            $two=round($one/1000*100,2);
            //每日一言
            $wenan=Db::name('wenan')
                ->orderRand()
                ->value('content');

            $daka=getCycleInfo($num);
            //处于什么时期
            $stage=Db::name('smoke_stage')
                ->where('day',">=",$num)
                ->find();
            $stage['day']=$stage['day']-$num;
            //奖励
            $award=json_decode($user['award'],true);
            $result['award']=$award[$stage['id']-1];
            $result['day']=$num;
            $result['health']=$two;
            $result['stage']=$stage;
            $result['smoke_num']=$smoke_num;
            $result['cost']=$cost;
            $result['shouming']=$smoke_num*11;
            $result['days']=$daka;
            $result['wenan']=$wenan;
            $result['list']=$model;
            $result['begin_time']=date("Y年m月d日",$user['begin_time']);
            $result['is_today_daka']=Db::name('user_plan_record')
                ->where('user_id',$this->userId)
                ->whereTime('create_time','today')
                ->count();
            return show(1,'',$result);
        }
    }
    /**
     * 开始戒烟
     */
    public function add_plan(){
        $data=input('post.');
        try {
            validate(\app\common\validate\Plan::class)
                ->scene("plan")
                ->check($data);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $this->show(0, $e->getError());
        }
        Db::startTrans();
        try {
            $user=Db::name('user_plan')
                ->where('user_id',$this->userId)
                ->find();
            if ($user){
                Db::name('user_plan')
                    ->where('user_id',$this->userId)
                    ->delete();
                Db::name('user_plan_record')
                    ->where('user_id',$this->userId)
                    ->delete();
            }
            $result['user_id']=$this->userId;
            $result['type']=1;
            $result['award']=json_encode($data['award']);
            $result['smoke_num']=$data['smoke_num'];
            $result['cost']=$data['cost'];
            $result['begin_time']=time();
            $result['create_time']=time();
            $result['update_time']=time();
            Db::name('user_plan')->insert($result);
            Db::name('user_plan_record')->insert(['user_id'=>$this->userId,'create_time'=>time(),'update_time'=>time()]);
            Db::commit();
            return show(1,'戒烟开始');
        }catch (\exception $e){
            Db::rollback();
            return show(0,'请重试');
        }
    }
    /**
     * 打卡
     */
    public function add_card(){
        $user=Db::name('user_plan_record')
            ->where('user_id',$this->userId)
            ->whereTime('create_time','today')
            ->find();
        if ($user){
            return show(0,'今日已打过卡');
        }
        $res=Db::name('user_plan_record')
            ->insert(['user_id'=>$this->userId,'create_time'=>time(),'update_time'=>time()]);
        if ($res){
            $data['img']=Db::name('img')
                ->orderRand()
                ->value('url');
            $data['wenan']=Db::name('wenan')
                ->orderRand()
                ->value('content');
            return $this->show(1,'',$data);
        }
        return $this->show(0,'打卡失败,请稍后重试');
    }
}